package com.zxg.zbc.dal.dao.jdbc;



import java.util.Date;

import com.taobao.common.dao.persistence.DBRoute;
import com.zxg.jdbc.CommonJDBCBaseDao;
import com.zxg.zbc.dal.dao.UserDao;
import com.zxg.zbc.dal.dao.exception.DAOException;
import com.zxg.zbc.dal.dataobject.User;
import com.zxg.zbc.dal.query.CommonQuery;
/**
 * 为了节省时间 才用jdbc做的
 * @author hd
 *
 */
public class UserDaoImpl extends CommonJDBCBaseDao<User> implements UserDao{



	@Override
	public User getUserByMobile(String mobile)
			throws DAOException {
		String sql = "select * from "+User.TABLE_NAME+" where mobile = ?";
		try {
			return this.getSingleResult(DBRoute.getAssistantRoute(), User.class,
					sql, mobile);
		} catch (com.taobao.common.dao.persistence.exception.DAOException e) {
			log.error("error getById ",e);
			throw new DAOException("error getById",e);
		}
	}

	@Override
	public User getUserBySign(String sign) throws DAOException {
		String sql = "select * from "+User.TABLE_NAME+" where sign = ?";
		try {
			return this.getSingleResult(DBRoute.getAssistantRoute(), User.class,
					sql, sign);
		} catch (com.taobao.common.dao.persistence.exception.DAOException e) {
			log.error("error getById ",e);
			throw new DAOException("error getById",e);
		}
	}

	@Override
	public User getUserById(Long id) throws DAOException {
		String sql = "select * from "+User.TABLE_NAME+" where id = ?";
		try {
			return this.getSingleResult(DBRoute.getAssistantRoute(), User.class,
					sql, id);
		} catch (com.taobao.common.dao.persistence.exception.DAOException e) {
			log.error("error getById ",e);
			throw new DAOException("error getById",e);
		}
	}

	@Override
	public long insertUser(User user) throws DAOException {
		user.setGmtCreated(new Date());
		user.setGmtModified(new Date());
		long id = this._insert(user);
		return id;
	}

	@Override
	public long updateUser(User user) throws DAOException {
		user.setGmtModified(new Date());
		long num = this._update(user);
		
		return num ;
	}

	@Override
	public CommonQuery<User> getUsersByQuery(CommonQuery<User> query)
			throws DAOException {
		return this.query(query, User.TABLE_NAME, User.class);
	}




	

}
